package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Result;
import cc.eddic.pratice.programmingparadigms.emp02list.ResultList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;

public class ResultListLxImpl implements ResultList {
    private static final Logger log = LoggerFactory.getLogger(ResultListGcsImpl.class);

    private Result[] results;


    public ResultListLxImpl(int capacity) {
        results = new Result[capacity];
    }
    public ResultListLxImpl() {
        this(10);
    }

    public ResultListLxImpl(Result[] results) {
        this(results.length * 2);
        // TODO:
        for(Result result:results){
            this.add(result);
        }
    }

    public ResultListLxImpl(ResultList results) {
        this(results.size() * 2);
        // TODO:
        for(int i=0;i<results.size();i++){
            this.add(results.get(i));
        }
    }

    @Override
    public void add(Result result) {
        if (size() == results.length) {
            Result[] newResults = new Result[results.length*2];
            System.arraycopy(results, 0, newResults, 0, size());
            results=newResults;
            results[size()] = result;
            if (log.isDebugEnabled()) log.debug("超出数组长度，进行扩容长度为 {}！！",newResults.length);
        } else {
            results[size()] = result;
            if (log.isDebugEnabled()) log.debug("添加成功！！");
        }
    }

    @Override
    public Result get(int index) throws IndexOutOfBoundsException{
        if(index>size()-1) {
            throw new IndexOutOfBoundsException("Index: "+index+", Size: "+ size());
        }
        return results[index];

    }

    @Override
    public int size() {
        int count = 0;
        for(Result result:results){
            if(result!=null){
                count++;
            }
        }
        return count;
    }

    @Override
    public Result findByGender(String gender) {
        for(Result result:results){
            if(result == null){
                continue;
            }
            if(result.getKey().equals(gender)){
                return result;
            }
        }
        return null;
    }

    @Override
    public Result findByDepartment(String department) {
        for(Result result:results){
            if(result == null){
                continue;
            }

            if(result.getKey().equals(department)){
                return result;
            }
        }
        return null;
    }

    @Override
    public Result findByJobTitle(String jobTitle) {
        for(Result result:results){
            if(result == null){
                continue;
            }
            if (log.isDebugEnabled()) log.debug("result is {}！！",result);
            if(result.getKey().equals(jobTitle)){
                return result;
            }
        }
        return null;
    }

}
